package com.example.UI;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;


import com.example.UI.database.MySQLDataHelper;
import com.example.UI.entity.User;
import com.example.UI.list.StudentArrayAdapter;
import com.example.ui.R;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    private Button mButtonStudents;
    private Button mButtonCourses;
    private Button mButtonProfile;
    private ListView listView;
    private MySQLDataHelper dbHelper;
    private List<User> users;
    private StudentArrayAdapter userArrayAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_student);

        dbHelper = new MySQLDataHelper(this);
        listView = findViewById(R.id.listView);

        users = dbHelper.getAllUsers();
        userArrayAdapter = new StudentArrayAdapter(this, users);
        listView.setAdapter(userArrayAdapter);


        mButtonStudents = findViewById(R.id.buttonStudents);
        mButtonStudents.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "点击了学生按钮", Toast.LENGTH_SHORT).show();
            }
        });

        mButtonCourses = findViewById(R.id.buttonCourses);
        mButtonCourses.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent courseIntent = new Intent(MainActivity.this, CourseActivity.class);
                startActivity(courseIntent);
            }
        });

        mButtonProfile = findViewById(R.id.buttonProfile);
        mButtonProfile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String studentId = getCurrentStudentId();
                if (studentId != null) {
                    Intent profileIntent = new Intent(MainActivity.this, InformationActivity.class);
                    profileIntent.putExtra("studentId", studentId);
                    startActivity(profileIntent);
                } else {
                    Toast.makeText(MainActivity.this, "未能获取学号，请重新登录", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private String getCurrentStudentId() {
        SharedPreferences sharedPreferences = getSharedPreferences("user_session", MODE_PRIVATE);
        return sharedPreferences.getString("studentId", null);
    }

}